/**
 * 
 */
package com.jfinal.server;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;


/**
 * @author y.z
 */
@SuppressWarnings("hiding")
public class BaseService <T extends Model<T>>{
	private Class<T> clazz; // 对应的实体

	@SuppressWarnings("unchecked")
	public BaseService() {
		 //当前对象的直接超类的 Type  
         if(null == clazz){
        	 Type genericSuperclass = getClass().getGenericSuperclass();  
             if(genericSuperclass instanceof ParameterizedType){  
                 //参数化类型  
                 ParameterizedType parameterizedType= (ParameterizedType) genericSuperclass;  
                 //返回表示此类型实际类型参数的 Type 对象的数组  
                 Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();  
                 this.clazz= (Class<T>)actualTypeArguments[0];  
             }else{  
                 this.clazz= (Class<T>)genericSuperclass;  
             }
         }
	}
	
	private T result;
	private List<T> resultList;
	private Page<T> resultPage;
	private boolean resultFlag;
	
	/**
	 * 根据id获取
	 * @param id
	 * @return
	 */
	@SuppressWarnings({ "unchecked" })
	protected <T extends Model<T>> T findById(Object id){
		//return c.newInstance().findById(id);
		result = null;
		try {
			result = clazz.newInstance().findById(id);
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (T) result;
		
		
		
	}
	
	/**
	 * 根据sql获取list集合
	 * @param sql
	 * @return
	 */
	@SuppressWarnings({ "unchecked"})
	protected <T extends Model<T>> List<T> find(String sql){
		resultList = null;
		try {
			resultList =  clazz.newInstance().find(sql);
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (List<T>) resultList;
		
		
	}
	
	/**
	 * 根据sql获取第一条数据
	 * @param sql
	 * @return
	 */
	@SuppressWarnings({ "unchecked" })
	protected <T extends Model<T>> T findFirst(String sql){
		result = null;
		try {
			return (T) clazz.newInstance().findFirst(sql);
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (T) result;
		
		
	}
	
	/**
	 * 获取分页数据
	 * @param pageNumber
	 * @param pageSize
	 * @param select
	 * @param sql
	 * @return
	 */
	@SuppressWarnings({ "unchecked" })
	protected <T extends Model<T>> Page<T> getPage(int curPage,int pageSize,String select,String sql){
		resultPage = null;
		try {
			resultPage = clazz.newInstance().paginate(curPage, pageSize, select, sql);
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (Page<T>) resultPage;
	}
	
	/**
	 * 获取分页数据，显示条数默认为10
	 * @param pageNumber
	 * @param select
	 * @param sql
	 * @return
	 */
	@SuppressWarnings({ "unchecked" })
	protected <T extends Model<T>> Page<T> getPage(int curPage,String select,String sql){
		resultPage = null;
		try {
			resultPage = clazz.newInstance().paginate(curPage, 10, select, sql);
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (Page<T>) resultPage;
	}
	
	/**
	 * 添加
	 * @param newModel
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected <T extends Model<T>> boolean save(Object newModel){
		
		/*if(newClazz instanceof Model){
			System.out.println(true);
		}else{
			System.out.print(false);
		}*/
		return ((Model<T>) newModel).save();
		
	}
	
	/**
	 * 更新
	 * @param updateModel
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected <T extends Model<T>> boolean update(Object updateModel){
		return ((Model<T>) updateModel).update();
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	protected <T extends Model<T>> boolean delById(Object id){
		try {
			resultFlag = clazz.newInstance().deleteById(id);
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultFlag;
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	protected <T extends Model<T>> boolean delete(Model<T> newModel){
		resultFlag = newModel.delete();
		return resultFlag;
	}
	
	/**
	 * SQL更新
	 * @param sql
	 * @return
	 */
	protected boolean update(String sql){
		
		if(Db.update(sql) > 0){
			resultFlag = true;
		}
		return resultFlag;
	}
	
}
